package com.i2works.smartluolong.api.controller.court;
import com.i2works.smartluolong.api.utils.BaseModule;
import com.i2works.smartluolong.utils.base.MsgEnum;
import com.i2works.smartluolong.utils.entity.R;
import com.i2works.smartluolong.utils.entity.Result;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.web.entity.court.*;
import com.i2works.smartluolong.web.service.court.*;
import com.xiaoleilu.hutool.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
 *
 * 法院api 申请
 *
 * @author Li haoyu
 * */
@RestController
@RequestMapping("/courtLawsuit")
public class CourtLawsuitControllers extends BaseModule {
    @Autowired
    ICourtAssetService assetService;
    @Autowired
    ICourtHouseService houseService;
    @Autowired
    ICourtLawsuitService lawsuitService;
    @Autowired
    ICourtCarService carService;
    @Autowired
    ICourtRelativesService relativesService;
    @Autowired
    ICourtOtherService iCourtOtherService;
    @Autowired
    ICourtSuggestService suggestService;



    /**
     * 婚姻基本信息申报
     */
    @RequestMapping("/info")
    public Result saveLawsuit(CourtLawsuitDO courtLawsuitDO) {
        if (null == courtLawsuitDO) {
            return R.error(MsgEnum.ATTRS_EMPTY.getMsg());
        }
        try {
            courtLawsuitDO.setUserId(getUserId());
            courtLawsuitDO.setCreateName(getUserName());
            courtLawsuitDO.setState(1);
            courtLawsuitDO.setStage(0);
            courtLawsuitDO.setUpdateName(getUserName());
            courtLawsuitDO.setHaveAsset(0);
            courtLawsuitDO.setHaveCar(0);
            courtLawsuitDO.setHaveHouse(0);
            courtLawsuitDO.setHaveSon(0);
            lawsuitService.save(courtLawsuitDO);
            CourtSuggestDO suggestDO=new CourtSuggestDO();
            suggestDO.setLawsuitId(courtLawsuitDO.getId());
            suggestDO.setStage(0);
            suggestDO.setSuggest("用户《"+getUserName()+"》新增申请");
            suggestService.save(suggestDO);
            return R.success(courtLawsuitDO);
        } catch (Exception e) {
            logger.error("增加婚姻申报信息出错:" + e.getMessage());
            return R.error(MsgEnum.SAVE_ERROR.getMsg());
        }
    }

    /**
     * 财产信息
     *
     */
    @RequestMapping("/asset/{lawsuitId}")
    public Result saveAsset(@RequestBody List<CourtAssetDO> assetDOList, @PathVariable(value = "lawsuitId") Integer lawsuitId) throws ServiceException {
        CourtLawsuitDO courtLawsuitDO = new CourtLawsuitDO();
        courtLawsuitDO.setId(lawsuitId);
        if (CollectionUtil.isEmpty(assetDOList)) {
            CourtAssetQuery assetQuery = new CourtAssetQuery();
            assetQuery.setLawsuitId(lawsuitId);
            List<CourtAssetDO> courtAssetDOList = assetService.queryList(assetQuery);
            if (CollectionUtil.isEmpty(courtAssetDOList)) {
                courtLawsuitDO.setHaveAsset(0);
                lawsuitService.update(courtLawsuitDO);
                return R.success(lawsuitId);
            } else {
                courtLawsuitDO.setHaveAsset(1);
                lawsuitService.update(courtLawsuitDO);
                return R.success(lawsuitId);
            }
        }
        try {
            assetService.deleteForLawsuit(lawsuitId);
            for (CourtAssetDO assetDO : assetDOList
            ) {
                assetDO.setUserId(getUserId());
                assetDO.setCreateName(getUserName());
                assetService.save(assetDO);
            }
            courtLawsuitDO.setHaveAsset(1);
            lawsuitService.update(courtLawsuitDO);
            return R.success(lawsuitId);
        } catch (Exception e) {
            logger.error("增加财产信息出错:" + e.getMessage());
            return R.error();
        }
    }

    /**
     * 子女信息
     */
    @RequestMapping("/relatives/{lawsuitId}")
    public Result saveRelatives(@RequestBody List<CourtRelativesDO> relativesDOList, @PathVariable(value = "lawsuitId") Integer lawsuitId) throws ServiceException {
        CourtLawsuitDO courtLawsuitDO = new CourtLawsuitDO();
        courtLawsuitDO.setId(lawsuitId);
        if (CollectionUtil.isEmpty(relativesDOList)) {
            CourtRelativesQuery courtRelativesQuery=new CourtRelativesQuery();
            courtRelativesQuery.setLawsuitId(lawsuitId);
            List<CourtRelativesDO>courtRelativesDOS=relativesService.queryList(courtRelativesQuery);
            if(CollectionUtil.isEmpty(courtRelativesDOS)){
                courtLawsuitDO.setHaveSon(0);
                lawsuitService.update(courtLawsuitDO);
                return R.success(lawsuitId);
            }else {
                courtLawsuitDO.setHaveSon(1);
                lawsuitService.update(courtLawsuitDO);
                return R.success(lawsuitId);
            }

        }
        try {
            relativesService.deleteForLawsuit(lawsuitId);
            for (CourtRelativesDO courtRelativesDO : relativesDOList
            ) {
                courtRelativesDO.setUserId(getUserId());
                courtRelativesDO.setCreateName(getUserName());
                relativesService.save(courtRelativesDO);
            }
            courtLawsuitDO.setHaveSon(1);
            lawsuitService.update(courtLawsuitDO);
            return R.success(lawsuitId);
        } catch (Exception e) {
            logger.error("增加子女信息出错:" + e.getMessage());
            return R.error();
        }
    }

    /**
     * 车辆信息
     */
    @RequestMapping("/car/{lawsuitId}")
    public Result saveCar(@RequestBody List<CourtCarDO> carDOList, @PathVariable(value = "lawsuitId") Integer lawsuitId) throws ServiceException {
        CourtLawsuitDO courtLawsuitDO = new CourtLawsuitDO();
        courtLawsuitDO.setId(lawsuitId);
        if (CollectionUtil.isEmpty(carDOList)) {
            CourtCarQuery carQuery=new CourtCarQuery();
            carQuery.setLawsuitId(lawsuitId);
            List<CourtCarDO>carDOList1=carService.queryList(carQuery);
            if(CollectionUtil.isEmpty(carDOList1)){
                courtLawsuitDO.setHaveCar(0);
                lawsuitService.update(courtLawsuitDO);
                return R.success(lawsuitId);
            }else {
                courtLawsuitDO.setHaveCar(1);
                lawsuitService.update(courtLawsuitDO);
                return R.success(lawsuitId);
            }

        }
        try {
            carService.deleteForLawsuit(lawsuitId);
            for (CourtCarDO carDO : carDOList
            ) {
                carDO.setUserId(getUserId());
                carDO.setCreateName(getUserName());
                carService.save(carDO);
            }
            courtLawsuitDO.setHaveCar(1);
            lawsuitService.update(courtLawsuitDO);
            return R.success(lawsuitId);
        } catch (Exception e) {
            logger.error("增加车辆信息出错:" + e.getMessage());
            return R.error();
        }
    }

    /**
     * 房产信息
     */
    @RequestMapping("/house/{lawsuitId}")
    public Result saveHouse(@RequestBody List<CourtHouseDO> houseDOList, @PathVariable(value = "lawsuitId") Integer lawsuitId) throws ServiceException {
        CourtLawsuitDO courtLawsuitDO = new CourtLawsuitDO();
        courtLawsuitDO.setId(lawsuitId);
        if (CollectionUtil.isEmpty(houseDOList)) {
            CourtHouseQuery courtHouseQuery=new CourtHouseQuery();
            courtHouseQuery.setLawsuitId(lawsuitId);
            List<CourtHouseDO>courtHouseDOS=houseService.queryList(courtHouseQuery);
            if(CollectionUtil.isEmpty(courtHouseDOS)){
                courtLawsuitDO.setHaveHouse(0);
                lawsuitService.update(courtLawsuitDO);
                return R.success(lawsuitId);
            }else {
                courtLawsuitDO.setHaveHouse(1);
                lawsuitService.update(courtLawsuitDO);
                return R.success(lawsuitId);
            }

        }
        try {
            houseService.deleteForLawsuit(lawsuitId);
            for (CourtHouseDO courtHouseDO : houseDOList
            ) {
                courtHouseDO.setUserId(getUserId());
                courtHouseDO.setCreateName(getUserName());
                houseService.save(courtHouseDO);

            }
            courtLawsuitDO.setHaveHouse(1);
            lawsuitService.update(courtLawsuitDO);
            return R.success(lawsuitId);
        } catch (Exception e) {
            logger.error("增加房产信息出错:" + e.getMessage());
            return R.error();
        }
    }

    /**
     * 其他财产
     */
    @RequestMapping("/other/{lawsuitId}")
    public Result other(@RequestBody List<CourtOtherDO> otherDO, @PathVariable(value = "lawsuitId") Integer lawsuitId) throws ServiceException {
        if (CollectionUtil.isEmpty(otherDO)) {
            return R.success("无其他财产");
        }
        try {
            iCourtOtherService.deleteForLawsuit(lawsuitId);
            for (CourtOtherDO courtOtherDO : otherDO
            ) {
                courtOtherDO.setUserId(getUserId());
                courtOtherDO.setLawsuitId(lawsuitId);
                iCourtOtherService.save(courtOtherDO);
            }
            return R.success(lawsuitId);
        } catch (Exception e) {
            logger.error("录入其他财产出错:" + e.getMessage());
            return R.error("增加其他财产出错:" + e.getMessage());
        }
    }

    /**
     * 申报告知
     */
    @RequestMapping("/notify/{lawsuitId}")
    public Result notifys(@PathVariable(value = "lawsuitId") Integer lawsuitId) {
        CourtLawsuitQuery courtLawsuitQuery = new CourtLawsuitQuery();
        courtLawsuitQuery.setId(lawsuitId);
        try {
            CourtLawsuitDO courtLawsuitDO = lawsuitService.query(courtLawsuitQuery);
            return  R.success(courtLawsuitDO);
        } catch (Exception e) {
            e.getMessage();
            return R.error("申报告知出错");
        }
    }

}
